<?php
$loader->requireOnce('includes/Utils.class.php');
class DepartmentEmployee extends ORDataObject {
	var $department_employee_id	= 	'';
	var $person_id				=	'';
  	var $department_id			=	'';
  	var $title_id				=	'';
  	var $from_date				=	'';
  	var $to_date				=	'';

	var $_table 		= 'department_employee';
	var $_key 			= 'department_employee_id';
	var $_internalName	= 'DepartmentEmployee';
	
	function DepartmentEmployee() {
		parent::ORDataObject();
	}

	public function setup($id = 0){
		$this->set('id',$id);
		if($id > 0){
			$this->populate();
		}
	}
	
	public function populate(){
		parent::populate('department_employee_id');
	}
	
	public function getId(){
		return $this->department_employee_id;
	}
	
	public function setId($id){
		$this->department_employee_id	=	$id;
	}
	
	public function getPersonId(){
		return $this->person_id;
	}
	
	public function getDeparmentId(){
		return $this->department_id;
	}
	
	public function getTitleId(){
		return $this->title_id;
	}
	
	public function getFromDate($format='%d/%m/%Y'){
		$date = DateObject::createFromISO($this->from_date);
        return Utils::trimDate('/', $date->toString($format));
	}
	

	public function getToDate($format='%d/%m/%Y'){
		$date = DateObject::createFromISO($this->to_date);
        return Utils::trimDate('/', $date->toString($format));
	}
	
	public function setData($person_id, $department_id, $title_id, 
		$from_date, $to_date = ''){
		$data	=	array(
			'person_id'		=>	$person_id,
			'department_id'	=>	$department_id,
			'title_id'		=>	$title_id,
			'from_date'		=>	$from_date,
			'to_date'		=>	$to_date
		);
		$this->populateArray($data);
	}
	
	public function checkDuplicated(){
		$ds =& new Datasource_sql();
        $sql = array(
            'cols'	=> "department_employee_id as id",
            'from'	=> "$this->_table",
        	'where'	=> "person_id=$this->person_id AND department_id=$this->department_id".
        		" AND title_id=$this->title_id AND from_date='$this->from_date'"
		);
        $ds->setup($this->_db,$sql);
        $res=$ds->toArray();
      	if(!empty($res))
      		return $res['0']['id'];
      	return 0;
	}
	
}
?>
